﻿
package extremefx.tools {
	import flash.utils.setTimeout;	
	import flash.utils.clearTimeout;	
	
	/**
	 * @author Marcelo Volmaro
	 */
	public final class Debounce {
		public static function create(pHandler:Function, pThreshold:uint = 100):Function{
			var timeout:uint;
			
			return function (...args):void {
		        function delayed ():void {
		            pHandler.apply(null, args);
		            timeout = 0;
		        };
		
		        if (timeout) clearTimeout(timeout);
		        timeout = setTimeout(delayed, pThreshold);
		    };
		}
	}
}
